/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui.actions;

import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.AbstractAction;

import com.lunarshades.samsara.Raider;

/**
 * Provides support of managing RosterChangeListeners and dispatching
 * RosterChangeEvents.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 */
public abstract class RosterChangeAction extends AbstractAction {

protected RosterChangeAction() {
    super();
    mListeners = new LinkedHashSet<RosterChangeListener>();
}

protected RosterChangeAction(String name) {
    super(name);
    mListeners = new LinkedHashSet<RosterChangeListener>();
}

public void addRosterChangeListener(RosterChangeListener listener) {
    if (listener == null) {
        throw new NullPointerException();
    }
    synchronized (mListeners) {
        mListeners.add(listener);
    }
}

public void removeRosterChangeListener(RosterChangeListener listener) {
    synchronized (mListeners) {
        mListeners.remove(listener);
    }
}

public <E extends Raider> void fireRosterChanged(E subject,
                                                 RosterChangeEvent.RosterChangeType type
) {
    final RosterChangeEvent EVENT = new RosterChangeEventImpl<E>(subject, type);
    synchronized (mListeners) {
        for (RosterChangeListener listener : mListeners) {
            listener.rosterChanged(EVENT);
        }
    }
}

private class RosterChangeEventImpl<S extends Raider>
        extends RosterChangeEvent<S> {
    private RosterChangeEventImpl(S subject, RosterChangeType type) {
        super(subject, type);
        mMessage = initMessage();
    }

    private String initMessage() {
        // ADDED [Person] karma [2]
        // REMOVED [Person] karma [2]
        // CHANGE [Person] <context>
        StringBuffer buffer = new StringBuffer();

        switch (type()) {
        case ADD:
            buffer.append("ADDED ");
            break;
        case REMOVE:
            buffer.append("REMOVED ");
            break;
        // MODIFY not yet supported
        default:
            break;
        }

        // person
        buffer.append("[").append(subject().name()).append("] ");

        // karma amount
        buffer.append("karma [");
        buffer.append(subject().karma().toString()).append("] ");

        return buffer.toString();
    }

    public Object message() {
        return mMessage;
    }

    private final String mMessage;
}

private final Set<RosterChangeListener> mListeners;
}
